<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------

namespace app\admin\service\member;

use app\api\model\member\Member;
use app\dict\member\MemberRegisterChannelDict;
use app\dict\member\MemberRegisterTypeDict;


use app\service\core\member\CoreMemberService;
use app\core\base\BaseAdminService;
use app\core\exception\AdminException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;


/**
 * 会员服务层
 * Class MemberService
 * @package app\service\admin\member
 */
class MemberService extends BaseAdminService
{
     public function __construct()
     {
          parent::__construct();
          $this->model = new Member();
     }

     /**
      * 会员列表
      * @param array $where
      * @return array
      */
     public function getPage(array $where = [])
     {

          $field = 'member_id, member_no, mobile, register_channel, nickname, headimg,sex, login_ip, login_time, create_time, last_visit_time, status, balance';

          $search_model = $this->model->withSearch(['keyword', 'create_time', 'register_channel', 'status'], $where)
               ->whereBetweenTime('create_time', strtotime($where['start_time']), strtotime($where['end_time']) + 86399)->field($field)->order('member_id desc')->append(['register_channel_name', 'sex_name', 'status_name']);
          $rs = $this->pageQuery($search_model);
          $rs['list'] = $rs['data'];
          unset($rs['data']);
          return $rs;
     }

     /**
      * 查询会员列表
      * @param array $where
      * @return array
      * @throws DbException
      * @throws DataNotFoundException
      * @throws ModelNotFoundException
      */
     public function getList(array $where = [])
     {
          $field = 'member_id, nickname, headimg';
          return $this->model->withSearch(['keyword'], $where)->field($field)->order('member_id desc')->limit($this->getPageParam()['limit'] ?? 0)->select()->toArray();
     }

     /**
      * 会员详情
      * @param int $member_id
      * @return array|null
      */
     public function getInfo(int $member_id)
     {
          $field = 'member_id,member_no, username, mobile, password, register_channel, register_type, nickname, headimg, member_level, member_label, wx_openid, weapp_openid, wx_unionid, ali_openid, douyin_openid, login_ip, login_type, login_channel, login_count, login_time, create_time, last_visit_time, last_consum_time, sex, status, birthday, point, point_get, balance, balance_get, growth, growth_get, is_member, member_time, is_del, province_id, city_id, district_id, address, location, delete_time, money, money_get, commission, commission_get, commission_cash_outing';
          return $this->makeUp($this->model->where([['member_id', '=', $member_id]])->field($field)->append(['register_channel_name', 'register_type_name', 'sex_name', 'login_channel_name', 'login_type_name', 'status_name'])->findOrEmpty()->toArray());
     }


     /**
      * 更新会员
      * @param int $member_id
      * @param array $data
      * @return true
      */
     public function edit(int $member_id, array $data)
     {
          $where = array(
               ['member_id', '=', $member_id],
          );

          $this->model->where($where)->update($data);
          return true;
     }

     /**
      * 修改字段
      * @param int $member_id
      * @param string $field
      * @param $data
      */
     public function modify(int $member_id, string $field, $data)
     {
          return (new CoreMemberService())->modify($member_id, $field, $data);
     }

     /**
      * 组合整理数据
      * @param $data
      */
     public function makeUp($data)
     {
          //会员标签
          if (!empty($data['member_label'])) {
               $data['member_label_array'] = (new MemberLabelService())->getMemberLabelListByLabelIds($data['member_label']);
          }
          return $data;
     }

     /**
      * 会员数量
      * @return int
      * @throws DbException
      */
     public function getCount(array $where = [])
     {
          $where[] = ['is_del', '=', 0];
          return $this->model->where($where)->count();
     }

     /**
      * 设置状态
      * @param string $member_id
      * @param int $status
      * @return true
      */
     public function setStatus(string $member_id, int $status)
     {
          $where = array(
               ['member_id', '=', $member_id],
          );
          $data = array(
               'status' => $status
          );
          $this->model->where($where)->update($data);
          return true;
     }

     /**
      * 会员数据
      * @param $field
      * @return float
      */
     public function getSum($field)
     {
          return $this->model->sum($field);
     }

     /**
      * 创建会员编码
      * @return string|null
      */
     public function getMemberNo()
     {
          return (new CoreMemberService())->createMemberNo();
     }

     /**
      * 删除会员
      * @param int $member_id
      * @return true
      */
     public function deleteMember(int $member_id)
     {
          $this->model->destroy(function ($query) use ($member_id) {
               $query->where([['member_id', '=', $member_id]]);
          });
          return true;
     }

}
